/** @file    Property.cpp
 *  @author  Alessandro Polo
 *  @version $Id: Property.cpp 2833 2010-08-02 23:12:57Z alex $
 *  @brief
 * File containing methods for the wosh::Property class.
 * The header for this class can be found in Property.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include <core/Property.h>
 #include <core/ObjectAllocator.h>
 #include <core/DataModels.h>
 #include <core/Utilities.h>


using namespace std;
namespace wosh {

 WOSH_REGISTER(wosh::Property, "wosh::ISerializable", 1.01, _static_Property)

const Property Property::EmptyProperty;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS

Property::Property() {
	clear();
 }

Property::Property( const std::string& Key ) {
	clear();
	this->key = Key;
 }

Property::Property( const std::string& Key, const Variant& Value ) {
	clear();
	this->key = Key;
	this->value = Value;
 }

Property::Property( const std::string& Key, const Variant& Value, const Permission& perm ) {
	clear();
	this->key = Key;
	this->value = Value;
	this->permission = perm;
 }

Property::Property( const Property& m ) {
	clear();
	*this = m;
 }

//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Property::clear() {
	this->key = "";
	this->value.clear();
	this->permission.clear();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////// OPERATORS

Property& Property::operator=(const Property& m) {
	if (this == &m) return *this; // same object?
	this->key = m.key;
	this->value = m.value;
	this->permission = m.permission;
	return *this;
 }

bool Property::operator==(const Property& other) const {
	if ( this->key != other.key ) return false;
	if ( this->value != other.value ) return false;
//	if ( this->permission != other.permission ) return false;
	return true;
 }

/////////////////////////////////////////////////////////////////////////////////////////////////// OPERATORS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::string Property::toStringUnixStyle() const {
	return this->permission.toUnixStyle() + "\t" + this->key + " = " + this->value.toString();
 }

std::string Property::toString() const {
	return Utilities::format("%s '%s'='%s' [%s]", getClassNameOnlyOf(getClassName()), this->key.c_str(), this->value.toString().c_str(), this->permission.toUnixStyle().c_str() );
 }

bool Property::updateHash( DataHashContext* hash_context ) const {
	if ( hash_context == NULL ) return false;
	bool ret = true;
	ret = hash_context->update( this->key ) & ret;
	ret = this->value.updateHash( hash_context ) & ret;
	ret = this->permission.updateHash( hash_context ) & ret;
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

DataFieldCollector<Property>* Property::getDataFields() {
	DataFieldCollector<Property>* fields = new DataFieldCollector<Property>();
	fields->add<const std::string&, Variant>(&Property::getKey, "Key", 0);
	fields->add<const Variant&, Variant>(&Property::getValue, "Value", 1);
	fields->add<const Permission&, Permission>(&Property::getPermission, "Permission", 2);
	return fields;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

}; // namespace wosh
